import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["queryArray", "value"];
import React from 'react';
import { Input, Select } from 'antd';
var Option = Select.Option;
/**
 *  组件需传值，1.一个数组 queryArray:[{queryLabel:''(必传),queryName:''(必传), inputProps: {}(可不传)}]
 *
 * @export
 * @class OptionInput
 * @extends {React.Component}
 */

var OptionInput = /*#__PURE__*/function (_React$Component) {
  _inherits(OptionInput, _React$Component);

  var _super = _createSuper(OptionInput);

  function OptionInput(props) {
    var _this;

    _classCallCheck(this, OptionInput);

    _this = _super.call(this, props);

    _this.handleSelectChange = function (optionValue) {
      var _this$props = _this.props,
          value = _this$props.value,
          _this$props$queryArra = _this$props.queryArray,
          queryArray = _this$props$queryArra === void 0 ? [] : _this$props$queryArra;
      var fieldValue = _this.state.fieldValue;
      var queryArrayItem = queryArray.filter(function (o) {
        return o.queryName === optionValue;
      });
      var inputProps = queryArrayItem[0].inputProps;

      _this.setState({
        fieldName: optionValue,
        inputProps: inputProps
      }, function () {
        if (value) {
          _this.fireChange(_defineProperty({}, optionValue, fieldValue));
        }
      });
    };

    _this.handleInputChange = function (e) {
      var fieldName = _this.state.fieldName;

      _this.setState({
        fieldValue: e.target.value
      });

      _this.fireChange(_defineProperty({}, fieldName, e.target.value));
    };

    var _props$queryArray = props.queryArray,
        _queryArray = _props$queryArray === void 0 ? [] : _props$queryArray;

    _this.state = {
      fieldValue: null,
      fieldName: _queryArray[0].queryName,
      inputProps: _queryArray[0].inputProps // input框的特定属性

    };
    return _this;
  }

  _createClass(OptionInput, [{
    key: "fireChange",
    value: function fireChange(valueList) {
      var onChange = this.props.onChange;

      if (onChange) {
        onChange(valueList);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          _this$props2$queryArr = _this$props2.queryArray,
          queryArray = _this$props2$queryArr === void 0 ? [] : _this$props2$queryArr,
          value = _this$props2.value,
          otherProps = _objectWithoutProperties(_this$props2, _excluded);

      var inputProps = this.state.inputProps;
      var selectValue = queryArray[0].queryName;
      var inputValue = null;

      if (value && Object.keys(value).length >= 1) {
        // eslint-disable-next-line
        selectValue = Object.keys(value)[0];
        inputValue = value[selectValue];
      }

      var selectBefore = /*#__PURE__*/React.createElement(Select, {
        style: {
          minWidth: 80
        },
        defaultValue: selectValue,
        onChange: this.handleSelectChange
      }, queryArray.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          key: item.queryName,
          value: item.queryName
        }, item.queryLabel);
      }));
      return /*#__PURE__*/React.createElement(Input, Object.assign({}, otherProps, inputProps, {
        value: inputValue,
        addonBefore: selectBefore,
        onChange: this.handleInputChange
      }));
    }
  }]);

  return OptionInput;
}(React.Component);

export { OptionInput as default };